import { VuexModule, Module, Action, getModule } from 'vuex-module-decorators'
import store from '@/store'
import { getWorkbenchs, updateWorkbench, deleteWorkbench } from '@/api/workbench'

export interface IWorkbenchState {

}

@Module({ dynamic: true, store, name: 'workbench' })
class Workbench extends VuexModule implements IWorkbenchState {

    @Action
    public async getWorkbenchs() {
        return new Promise((r: any, j: any) => {
            getWorkbenchs({}).then((value: any) => {
                console.error("Workbench:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async updateWorkbench(obj: any) {
        return new Promise((r: any, j: any) => {
            updateWorkbench(obj).then((value: any) => {
                console.error("Workbench:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteWorkbench(workId: number) {
        return new Promise((r: any, j: any) => {
            deleteWorkbench({ workId: workId }).then((value: any) => {
                console.error("Workbench:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

}

export const WorkbenchModule = getModule(Workbench)
